package com.macro.mall.controller;

import com.github.pagehelper.PageHelper;
import com.macro.mall.annotationUtil.CurrentUser;
import com.macro.mall.annotationUtil.CurrentUserName;
import com.macro.mall.common.api.CommonPage;
import com.macro.mall.common.api.CommonResult;
import com.macro.mall.dto.OmsOrderReturnApplyResult;
import com.macro.mall.dto.OmsReturnApplyQueryParam;
import com.macro.mall.mapper.OmsOrderReturnApplyMapper;
import com.macro.mall.model.OmsOrderReturnApply;
import com.macro.mall.model.UmsAdmin;
import com.macro.mall.service.OmsOrderReturnApplyService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.util.List;

/**
 * 订单退货申请管理
 * Created by macro on 2018/10/18.
 */
@Controller
@Api(tags = "OmsOrderReturnApplyController", description = "订单退货申请管理")
@RequestMapping("/returnApply")
public class OmsOrderReturnApplyController {
    @Autowired
    private OmsOrderReturnApplyService returnApplyService;
    @Autowired
    private OmsOrderReturnApplyMapper returnApply;

    @ApiOperation("分页查询退款退货申请")
    @RequestMapping(value = "/list", method = RequestMethod.GET)
    @ResponseBody
    public CommonResult<CommonPage<OmsOrderReturnApply>> list(@CurrentUser UmsAdmin admin, OmsReturnApplyQueryParam queryParam,
                                                              @RequestParam(value = "pageSize") Integer pageSize,
                                                              @RequestParam(value = "pageNum") Integer pageNum) {
        CommonPage<OmsOrderReturnApply> list = returnApplyService.list(admin,queryParam, pageSize, pageNum);
        return CommonResult.success(list);
    }

    @ApiOperation("批量删除申请")
    @RequestMapping(value = "/delete", method = RequestMethod.POST)
    @ResponseBody
    public CommonResult delete(@RequestParam("ids") List<Long> ids) {
        int count = returnApplyService.delete(ids);
        if (count > 0) {
            return CommonResult.success(count);
        }
        return CommonResult.failed();
    }

    @ApiOperation("获取退货申请详情")
    @RequestMapping(value = "/{id}", method = RequestMethod.GET)
    @ResponseBody
    public CommonResult getItem(@PathVariable Long id) {
        OmsOrderReturnApplyResult result = returnApplyService.getItem(id);
        return CommonResult.success(result);
    }

    /**
     * 退货退款确认收货
     * @param currentUserName
     * @param id
     * @return
     */
    @ApiOperation("修改申请状态")
    @RequestMapping(value = "/confirm", method = RequestMethod.POST)
    @ResponseBody
    public CommonResult confirm(@CurrentUserName String currentUserName,
                                     @RequestParam Long id) {
        return returnApplyService.confirm(currentUserName,id);
    }
    @ApiOperation("同意买家的售后申请")
    @RequestMapping(value = "/agree", method = RequestMethod.POST)
    @ResponseBody
    public CommonResult agree(@RequestBody OmsOrderReturnApply returnApply) {
        return returnApplyService.agree(returnApply);
    }

    @ApiOperation("修改价格")
    @RequestMapping(value = "/updateAmount", method = RequestMethod.POST)
    @ResponseBody
    public CommonResult updateAmount(@RequestParam Double amount,@RequestParam Long id) {
        OmsOrderReturnApply apply = new OmsOrderReturnApply();
        apply.setReturnAmount(new BigDecimal(amount));
        apply.setId(id);
        int i = this.returnApply.updateByPrimaryKeySelective(apply);
        if(i>0){
            return  CommonResult.success(i);
        }else {
            return CommonResult.failed("操作失败，请稍后再试");
        }
    }
}
